package cn.jbolt.admin.sensitiveword;

import cn.jbolt.core.api.HttpMethod;
import cn.jbolt.core.api.JBoltApiBaseController;
import cn.jbolt.core.api.httpmethod.JBoltHttpGet;
import cn.jbolt.core.api.httpmethod.JBoltHttpMethod;
import cn.jbolt.core.api.httpmethod.JBoltHttpPost;
import cn.jbolt.core.model.SensitiveWord;
import cn.jbolt.core.model.base.JBoltBaseModel;
import cn.jbolt.core.para.JBoltNoUrlPara;
import cn.jbolt.core.permission.CheckPermission;
import cn.jbolt.core.permission.OnlySaasPlatform;
import cn.jbolt.core.permission.UnCheckIfSystemAdmin;
import cn.jbolt.core.sensitiveword.JBoltSensitiveWordUtil;
import cn.jbolt.core.service.JBoltSensitiveWordService;
import cn.jbolt.core.service.base.JBoltBaseService;
import cn.jbolt.extend.permission.PermissionKey;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.core.paragetter.Para;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Okv;
import com.jfinal.plugin.activerecord.tx.Tx;

@CheckPermission(PermissionKey.SENSITIVE_WORD)
@UnCheckIfSystemAdmin
@OnlySaasPlatform
@Path("/api/admin/sensitiveWord")
public class SensitiveWordAdminController extends JBoltApiBaseController {
    @Inject
    JBoltSensitiveWordService service;

    /**
     * 单个详情
     */
    @JBoltHttpGet
    @Before(JBoltNoUrlPara.class)
    public void index() {
        renderJBoltApiRet(findModelById());
    }

    /**
     * 统计数量
     */
    @JBoltHttpGet
    public void counts() {
        int totalCount = service.getCount();
        int enableCount = service.getCount(Okv.by("enable", TRUE));
        int disabledCount = service.getCount(Okv.by("enable", FALSE));
        Kv datas = Kv.create();
        datas.set("totalCount", totalCount);
        datas.set("enableCount", enableCount);
        datas.set("disabledCount", disabledCount);
        datas.set("loadedCount", JBoltSensitiveWordUtil.me.getCount());
        renderJBoltApiSuccessWithData(datas);
    }


    /**
     * 重新加载
     */
    @JBoltHttpGet
    @Before(JBoltNoUrlPara.class)
    public void reload() {
        JBoltSensitiveWordUtil.me.reload();
        renderJBoltApiSuccess();
    }

    /**
     * 测试
     */
    @JBoltHttpMethod({HttpMethod.GET,HttpMethod.POST})
    public void test(@Para("content")String content) {
        if (notOk(content)) {
            renderJBoltApiFail("请输入需要检测的内容");
            return;
        }
        renderJBoltApiSuccessWithData(JBoltSensitiveWordUtil.me.findAllWords(content));
    }

    /**
     * 分页条件查询
     */
    @JBoltHttpGet
    @Before(JBoltNoUrlPara.class)
    public void datas(@Para(value = PAGE_PARAM_NAME,defaultValue = "1")Integer page,
                      @Para(value = PAGESIZE_PARAM_NAME,defaultValue = "15")Integer pageSize,
                      @Para(KEYWORDS_PARAM_NAME)String keywords,
                      @Para(ENABLE_PARAM_NAME)Boolean enable) {
        renderJBoltApiSuccessWithData(service.paginateAdminDatas(page, pageSize, keywords, enable));
    }

    /**
     * 保存
     */
    @Before(Tx.class)
    @JBoltHttpPost
    public void save(@Para("")SensitiveWord sensitiveWord) {
        renderJBoltApiRet(service.save(sensitiveWord));
    }

    /**
     * 更新
     * @param sensitiveWord
     */
    @Before(Tx.class)
    @JBoltHttpPost
    public void update(@Para("")SensitiveWord sensitiveWord) {
        renderJBoltApiRet(service.update(sensitiveWord));
    }

    /**
     * 切换enable
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void toggleEnable() {
        renderJBoltApiRet(service.toggleEnable(getId()));
    }

    /**
     * 删除
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void delete() {
        renderJBoltApiRet(service.delete(getId()));
    }

    @Before({Tx.class, JBoltNoUrlPara.class})
    @JBoltHttpGet
    public void deleteByIds() {
        renderJBoltApiRet(service.deleteByIds(getIds()));
    }

    @Override
    protected JBoltBaseService<? extends JBoltBaseModel<?>> getModelService() {
        return service;
    }
}
